package com.woniuxy.project.campus.canteen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project.campus.AccountOpenFeign;


import com.woniuxy.project.campus.account.model.form.AccountForm;
import com.woniuxy.project.campus.canteen.model.AccountAddress;
import com.woniuxy.project.campus.canteen.mapper.AccountAddressMapper;
import com.woniuxy.project.campus.canteen.param.AddAddressParam;
import com.woniuxy.project.campus.canteen.param.UpdateAddressParam;
import com.woniuxy.project.campus.canteen.service.AccountAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.woniuxy.project.campus.canteenModelform.AddAddressForm;
import com.woniuxy.project.campus.util.Result;
import com.woniuxy.project.campus.util.StaticData;
import com.woniuxy.project.campus.util.TokenTool;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 猫将军
 * @since 2025年02月17日
 */
@Service
public class AccountAddressServiceImpl extends ServiceImpl<AccountAddressMapper, AccountAddress> implements AccountAddressService {

    @Resource
    private AccountAddressMapper accountAddressMapper;
    @Resource
    private AccountOpenFeign accountOpenFeign;


    @Override
    public void addAddress(AddAddressParam bean) {
        Integer id = TokenTool.getUserIdByToken(bean.getToken());
        //解析token
        Result account = accountOpenFeign.getUserInfoById(id);
        AccountForm bean1 = BeanUtil.toBean(account.getData(), AccountForm.class);
        if (bean1==null){
            throw new RuntimeException("未找到您的账号信息");
        }
        //查询已经存在的默认地址
        QueryWrapper<AccountAddress> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("account_id",id);
        queryWrapper1.eq("default_address",1);
        AccountAddress accountAddress1 = accountAddressMapper.selectOne(queryWrapper1);
        if (accountAddress1!=null){
            accountAddress1.setDefaultAddress(0);
            accountAddressMapper.updateById(accountAddress1);
        }


        //添加地
        AccountAddress accountAddress = new AccountAddress();
        accountAddress.setAccountId(id);
        accountAddress.setAddressName(bean.getAddressName());
        accountAddress.setPhone(bean.getPhone());
        accountAddress.setUserName(bean.getUserName());
        accountAddress.setDefaultAddress(bean.getDefaultAddress());
        accountAddressMapper.insert(accountAddress);
    }

    @Override
    public List<AccountAddress> getAddress(String token) {
        //验证token
        Integer id = TokenTool.getUserIdByToken(token);
        //解析token
        Result account = accountOpenFeign.getUserInfoById(id);
        AccountForm bean1 = BeanUtil.toBean(account.getData(), AccountForm.class);
        if (bean1==null){
            throw new RuntimeException("未找到您的账号信息");
        }
        QueryWrapper<AccountAddress> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("account_id",id);
        List<AccountAddress> accountAddresses = accountAddressMapper.selectList(queryWrapper1);
        return accountAddresses;
    }

    @Override
    public void deleteAddress(UpdateAddressParam bean) {
        Integer id = TokenTool.getUserIdByToken(bean.getToken());
        //解析token
        Result account = accountOpenFeign.getUserInfoById(id);
        AccountForm bean1 = BeanUtil.toBean(account.getData(), AccountForm.class);
        if (bean1==null){
            throw new RuntimeException("未找到您的账号信息");
        }
        QueryWrapper<AccountAddress> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",id);
        queryWrapper1.eq("account_id",id);
        AccountAddress accountAddress = accountAddressMapper.selectOne(queryWrapper1);
        queryWrapper1.clear();
        QueryWrapper<AccountAddress> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("account_id",id);
        List<AccountAddress> accountAddresses = accountAddressMapper.selectList(queryWrapper2);

        if(bean.getIsDefault()!=null){
            for (AccountAddress accountAddress1 : accountAddresses) {
                if(accountAddress1.getDefaultAddress()!=null){
                    accountAddress1.setDefaultAddress(null);
                    accountAddressMapper.updateById(accountAddress1);
                }
             }
        }


        accountAddress.setAddressName(bean.getAddress());
        accountAddress.setPhone(bean.getPhone());
        accountAddress.setUserName(bean.getName());
        accountAddress.setDefaultAddress(bean.getIsDefault());
        //修改
//        accountAddressMapper.update(accountAddress)
        accountAddressMapper.updateById(accountAddress);
    }

    @Override
    public void deleteAddress(String token, Integer addressId) {
        Integer id = TokenTool.getUserIdByToken(token);
        //解析token
        Result account = accountOpenFeign.getUserInfoById(id);
        AccountForm bean1 = BeanUtil.toBean(account.getData(), AccountForm.class);
        if (bean1==null){
            throw new RuntimeException("未找到您的账号信息");
        }
        //查询是否存在该地址
        QueryWrapper<AccountAddress> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",addressId);
        queryWrapper1.eq("account_id",id);
        AccountAddress accountAddress = accountAddressMapper.selectOne(queryWrapper1);
        if (accountAddress==null){
            throw new RuntimeException("未找到您的地址信息");
        }
        accountAddressMapper.deleteById(addressId);
    }

    @Override
    public void setDefaultAddress(String token, Integer addressId) {
        Integer id = TokenTool.getUserIdByToken(token);
        //解析token
        Result account = accountOpenFeign.getUserInfoById(id);
        AccountForm bean1 = BeanUtil.toBean(account.getData(), AccountForm.class);
        if (bean1==null){
            throw new RuntimeException("未找到您的账号信息");
        }
        QueryWrapper<AccountAddress> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",addressId);
        queryWrapper1.eq("account_id",id);
        AccountAddress accountAddress = accountAddressMapper.selectOne(queryWrapper1);
        if (accountAddress==null){
            throw new RuntimeException("未找到您的地址信息");
        }
        if(accountAddress.getDefaultAddress()!=null){
            throw new RuntimeException("该地址已经是默认地址");
        }
        QueryWrapper<AccountAddress> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("account_id",id);
        List<AccountAddress> accountAddresses = accountAddressMapper.selectList(queryWrapper2);
        if(accountAddress!=null){
            for (AccountAddress accountAddress1 : accountAddresses) {
                if(accountAddress1.getDefaultAddress()!=null){
                    accountAddress1.setDefaultAddress(null);
                    accountAddressMapper.updateById(accountAddress1);
                }
            }
        }
        //进行设置
        accountAddress.setDefaultAddress(StaticData.DEFAULT_ADDRESS);
        accountAddressMapper.updateById(accountAddress);
    }

    @Override
    public Integer getAddressId(AddAddressForm form) {
        QueryWrapper<AccountAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_name",form.getAddressName());
        queryWrapper.eq("phone",form.getPhone());
        queryWrapper.eq("user_name",form.getUserName());
        AccountAddress accountAddress = accountAddressMapper.selectOne(queryWrapper);
        return accountAddress.getId();
    }
}
